package 代码记录.校招笔试真题.笔试真题_2023.上海金融期货信息技术苏分所;

import java.util.*;

/**
 * @author zx
 * @create 2023-03-29 19:35
 * 100%
 */
public class Test3 {
    public String stringcnt (String str) {
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0;i < str.length();i++){
            if(map.containsKey(str.charAt(i))){
                map.put(str.charAt(i),map.get(str.charAt(i)) + 1);
            }else{
                map.put(str.charAt(i),1);
            }
        }
        List<Map.Entry<Character,Integer>> entries = new ArrayList<>(map.entrySet());
        Collections.sort(entries, new Comparator<Map.Entry<Character, Integer>>() {
            @Override
            public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
                if(o1.getValue() != o2.getValue()){
                    return o2.getValue() - o1.getValue();
                }else{
                    int a = (int)o1.getKey();
                    int b = (int)o2.getKey();
                    return a - b;
                }
            }
        });
        String res = new String();
        for(int i = 0;i < entries.size();i++){
            char c = entries.get(i).getKey();
            res += c;
        }
        return res;
    }
}
